package njust.edu.controller;

import njust.edu.dao.UserDao;
import njust.edu.dao.impl.UserDaoImpl;
import njust.edu.entity.*;

import njust.edu.service.*;


import njust.edu.service.impl.*;
import njust.edu.web.Cart;
import njust.edu.web.Page;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

@WebServlet(name = "MianController", value = "/MianController")
public class MianController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }

    public static String getType(Object object){
        String typeName=object.getClass().getName();
        int length= typeName.lastIndexOf(".");
        String type =typeName.substring(length+1);
        return type;
    }

    public void loginCheck(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        String idStr = request.getParameter("id");
        String password = request.getParameter("password");
        HttpSession session = request.getSession();

        if (idStr == null || password == null ) {
            response.sendRedirect("index.jsp");
            return;
        }
        PrintWriter out = response.getWriter();

        UserDao userdao = new UserDaoImpl();
//        User user = userdao.findByName(idStr);
        //System.out.println(getType(user));

//        UserDao userdao = new UserDaoImpl();
//        User user = userdao.findById(id);
//        out.println(user.getId()+" "+user.getPassword());
//        if(user.getPassword().equals(password))
//            result="1";
//        if(user.getId().equals(id))
//            result="1";

        boolean b = userService.checkUserPassword(idStr, password);
        //out.write(b+"");

        if(!b){
            res="login_false";
        }

        if(b){
            res="login_right";
            User user = userService.getUserByName(idStr);
            session.setAttribute("user", user);
            //response.sendRedirect("MianController?method=Login");
        }

        out.write(res);
       // System.out.println(res);
        out.close();
    }

    public void Login(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        //System.out.println(user.getName());
//        if (user == null) {
//            response.sendRedirect("index.jsp");
//            return;
//        }

        if (user.getType()==1){
            response.sendRedirect("BookController?method=allDishList");
        }
        if (user.getType()==2){
            response.sendRedirect("TradeController?method=toTradeManageUI");
        }
        if (user.getType()==3){
            response.sendRedirect("TradeController?method=toAcceptOrderUI");
        }


    }

    UserService userService = new UserServiceImpl();
    String res="1";

    public void SignUpCheck(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String pwd = request.getParameter("userpwd");
        String telphone =request.getParameter("tell");
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();



            User u = userService.getUserByName(username);
            if(u.getName()==null){
                userService.addUser(username,pwd,telphone);
                res="sign_right";
                //out.write(username+pwd+telphone);
                //response.sendRedirect("index.jsp?result=1");
            }

            else {
                System.out.println(username);
                //response.sendRedirect("index.jsp?result=0");
                res="Duplicate name";
            }




        out.write(res);
        // System.out.println(res);
        out.close();
    }
    public void SignUp(HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        response.sendRedirect("index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String methodName = request.getParameter("method");
        try {
            Method method = getClass().getDeclaredMethod(methodName,
                    HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            method.invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

